package leetcode.每日一题;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/21 12:14
 * @description：https://leetcode-cn.com/problems/invert-binary-tree/'
 */
public class 翻转二叉树 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public TreeNode invertTree(TreeNode root) {

            if (root == null) return null;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode cur = queue.poll();
                    TreeNode left = cur.left;
                    TreeNode right = cur.right;
                    cur.left = right;
                    cur.right = left;
                    if (cur.left != null) {
                        queue.offer(cur.left);
                    }
                    if (cur.right != null) {
                        queue.offer(cur.right);
                    }
                }
            }
            return root;
        }
    }

}
